package com.witte.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.witte.entity.RespBean;
import com.witte.entity.User;
import com.witte.service.UserService;



@Controller
@ResponseBody
@RequestMapping(value="/",produces="application/json;charset=UTF-8")
@CrossOrigin
public class UserController {
	@Autowired
	private UserService userService;
	
//	@RequestMapping(value = "/registe")
//	public RespBean registe(@Valid @RequestBody User user) {
//		if(userService.selectByPrimaryKey(user.getUsername())!=null)
//			return RespBean.error("账号已经存在！");
//		int rs=userService.insert(user);
//		return rs>0?RespBean.ok("注册成功!"):RespBean.error("注册失败！");
//	}
	
	@PostMapping(value = "/login")
	public RespBean login( @RequestBody User user, BindingResult bindingResult ,HttpServletRequest req) {
		System.out.println(user.getName());
		
		if (bindingResult.hasErrors()) {
            String message = String.format("登陆失败，详细信息[%s]。", bindingResult.getFieldError().getDefaultMessage());
            return RespBean.error(message);
        }

		User isOK=userService.login(user);
		RespBean result=null;
		if(isOK!=null) {
			isOK.setPassword("");
			result=RespBean.ok("登录成功！",isOK);
			req.getSession().setAttribute("username", user.getName());
			System.out.println("登录账号："+req.getSession().getAttribute("username"));
		}
		else
			result=RespBean.error("登陆失败:用户名或密码信息不正确。");
			
		return result;
	}
	
//	@RequestMapping(value = "/per/userInfo")
//	public RespBean getUserInfo(HttpServletRequest req) {
//		String username= (String) req.getSession().getAttribute("username");
//		if(username!=null && !username.equals("")) {
//			User user=userService.selectByPrimaryKey(username);
//			if(user.getUsername().equals(username)) {
//				user.setPassword("");
//				return RespBean.ok("",user);
//			}
//		}
//		return RespBean.build(203, "", null);
//	}
	
	@GetMapping(value = "/api/getRoleList")
	public RespBean getRoleList(int role) {
		List<User> list=userService.getRoleUser(role);
		RespBean respBean=RespBean.error("获取错误！");
		if(list!=null)
			respBean=RespBean.ok("", list);
		return respBean;
	}
	
	@RequestMapping(value = "/api/logout")
	public RespBean logout(HttpServletRequest req) {
		req.getSession().invalidate();
		return RespBean.ok("注销成功");
	}
	
	@RequestMapping(value = "/per/heartbeat")
	public RespBean heartbeat() {
		return RespBean.ok("");
	}	
	
	
}

